package com.uchedao.seller.ui.car.manage;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.uchedao.seller.R;
import com.uchedao.seller.config.Api;
import com.uchedao.seller.config.GlobalInfo;
import com.uchedao.seller.http.entity.BidPriceEntity;
import com.uchedao.seller.http.entity.CarBiddingBean;
import com.uchedao.seller.http.request.HttpRequest;
import com.uchedao.seller.http.response.BidPriceListResponse;
import com.uchedao.seller.ui.car.adapter.BidPriceAdapter;
import com.uchedao.seller.ui.car.base.BasePublishActivity;
import com.uchedao.seller.util.Arith;
import com.uchedao.seller.util.Utility;
import com.uchedao.seller.widget.dialog.ComfirmBidDialog;
import com.uchedao.seller.widget.library.PullToRefreshBase;
import com.uchedao.seller.widget.library.PullToRefreshScrollView;

import java.util.HashMap;
import java.util.Map;

/**
 * 查看出价
 */
public class ViewBidActivity extends BasePublishActivity implements PullToRefreshBase.OnRefreshListener2, AdapterView.OnItemClickListener {

    private PullToRefreshScrollView svRoot;
    private ListView lvPrice;

    private CarBiddingBean data;

    private boolean isClickable;    // true 可选择出价   false 不可

    @Override
    protected String getActivityTag() {
        return "ViewBidActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_view_bid;
    }

    @Override
    protected void initView() {
        initToBar("查看出价");

        svRoot = (PullToRefreshScrollView) findViewById(R.id.svViewBid);
        lvPrice = (ListView) findViewById(R.id.lvBidPrice);

        svRoot.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
    }

    @Override
    protected void initData() {
        data = (CarBiddingBean) getIntent().getSerializableExtra("data");
        isClickable = getIntent().getBooleanExtra("click", false);

        updateCarView(data);
        getDetailInfo();
        getBidPriceList();
    }

    @Override
    protected void initListener() {
        svRoot.setOnRefreshListener(this);
        if(isClickable)
            lvPrice.setOnItemClickListener(this);
    }

    /**
     * 竞拍车辆详情
     */
    public void getDetailInfo() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("app_name", GlobalInfo.APP_NAME);
        map.put("id", data.getId()+"");
        Request req = HttpRequest.getRequest(HttpRequest.GET, Api.Action.CAR_INFO, map, new Response.Listener<JsonObject>() {
            @Override
            public void onResponse(JsonObject response) {
                CarBiddingBean bean = new Gson().fromJson(response, CarBiddingBean.class);
                updateCarView(bean);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        addQueue(req);
    }

    /**
     * 出价列表
     */
    public void getBidPriceList() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("app_name", GlobalInfo.APP_NAME);
        map.put("session_car_id", data.getId()+"");
        Request req = HttpRequest.getRequest(HttpRequest.GET, Api.Action.BID_PRICE_LIST, map,
                new Response.Listener<JsonObject>() {
                    @Override
                    public void onResponse(JsonObject response) {
                        svRoot.onRefreshComplete();
                        BidPriceListResponse bean = new Gson().fromJson(response, BidPriceListResponse.class);
                        lvPrice.setAdapter(new BidPriceAdapter(ViewBidActivity.this, bean.getList()));
                        Utility.setListViewHeightBasedOnChildren(lvPrice);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        svRoot.onRefreshComplete();
                    }
                });
        addQueue(req);
    }

    public void updateCarView(CarBiddingBean data) {
        if(data == null)
            return;

        this.data = data;

        setImageByURL(R.id.ivCarItemPic, data.getCover_photo());
        setText(R.id.tvCarItemTitle, data.getTitle());
        setText(R.id.tvCarItemKmDate, Arith.div(data.getKilometer(), 10000) + "万公里 " + data.getCard_time());
        setText(R.id.tvCartItemReservePrice, (Arith.div(data.getReserve_price(), 10000) + ""));
        setText(R.id.tvCarItemMaxPrice, data.getBid_num() > 0 ? Arith.div(data.getMax_bid_price(), 10000) + "" : "--");
        setText(R.id.tvCarItemEndTime, data.getEnd_time() + "结束");
        setText(R.id.tvCarItemLookNum, data.getLook_num() + "人围观");
        setText(R.id.tvCarItemBidNum, data.getBid_num() + "人出价");
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        getDetailInfo();
        getBidPriceList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        BidPriceEntity item = (BidPriceEntity) adapterView.getItemAtPosition(i);
        ComfirmBidDialog dlg = new ComfirmBidDialog(this);
        dlg.data = item;
        dlg.show();
    }
}
